package com.icode.web.controller.manager;

import com.icode.core.dto.house.ForEnumDTO;
import com.icode.core.dto.manager.SellManagerDTO;
import com.icode.core.model.house.HouseBaseArea;
import com.icode.service.house.SellManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: xianfei
 * Date: 15-8-15
 * Time: 上午10:47
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/sell")
public class SellManagerController {

    @Autowired
    private SellManagerService sellManagerService;


    @RequestMapping(value = "form", method = RequestMethod.GET)
    private ModelAndView formShow(Model model) {
        List<ForEnumDTO> areas = HouseBaseArea.toDTOs();
        model.addAttribute("areas", areas);
        SellManagerDTO sellManagerDTO = new SellManagerDTO();
        return new ModelAndView("/back/sell/sell", "sellManagerDTO", sellManagerDTO);
    }

    @RequestMapping(value = "form", method = RequestMethod.POST)
    private String saveOrUpdate(@ModelAttribute(value = "sellManagerDTO") @Valid SellManagerDTO sellManagerDTO, BindingResult bindingResult, Model model) {
        if (bindingResult.hasErrors()) {
            List<ForEnumDTO> areas = HouseBaseArea.toDTOs();
            model.addAttribute("areas", areas);
            return "back/sell/sell";
        }
        sellManagerService.saveOrUpdateSell(sellManagerDTO);
        return "redirect:/sell/list";
    }

    @RequestMapping(value = "list", method = RequestMethod.GET)
    private String list(Model model) {
        List<SellManagerDTO> sellManagerDTOs = sellManagerService.findSellByAll();
        model.addAttribute("sellManagerDTOs", sellManagerDTOs);
        return "back/sell/sellUpdate";
    }

    @RequestMapping(value = "beforeUpdate/{sellGuid}", method = RequestMethod.GET)
    private String beforeUpdate(@PathVariable String sellGuid, Model model) {
        SellManagerDTO sellManagerDTO = sellManagerService.findSellByGuid(sellGuid);
        List<ForEnumDTO> areas = HouseBaseArea.toDTOs();
        model.addAttribute("areas", areas);
        model.addAttribute("sellManagerDTO", sellManagerDTO);
        return "back/sell/sell";
    }

    @RequestMapping(value = "show", method = RequestMethod.GET)
    private String show(Model model) {
        List<SellManagerDTO> sellManagerDTOs = sellManagerService.findSellByAll();
        model.addAttribute("sellManagerDTOs", sellManagerDTOs);
        return "back/sell/sellOverView";
    }

    @RequestMapping(value = "data_content/{sellGuid}", method = RequestMethod.GET)
    @ResponseBody
    private Object dataContent(@PathVariable String sellGuid) {
        SellManagerDTO sellManagerDTO = sellManagerService.findSellByGuid(sellGuid);
        return sellManagerDTO;
    }

    @RequestMapping(value = "operate/{sellGuid}/{type}", method = RequestMethod.GET)
    @ResponseBody
    private Object operate(@PathVariable String sellGuid, @PathVariable String type) {
        return sellManagerService.operateSell(sellGuid, type);
    }
}
